package com.jeegit.ecms.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jeegit.common.annotation.Log;
import com.jeegit.common.enums.BusinessType;
import com.jeegit.ecms.domain.CmsBannerCategory;
import com.jeegit.ecms.service.ICmsBannerCategoryService;
import com.jeegit.ecms.utils.ECMSUtils;
import com.jeegit.common.core.controller.BaseController;
import com.jeegit.common.core.domain.AjaxResult;
import com.jeegit.common.utils.poi.ExcelUtil;
import com.jeegit.common.utils.StringUtils;
import com.jeegit.common.core.domain.Ztree;

/**
 * 横幅图片Controller
 * 
 * @author jeegit
 * @date 2022-06-25
 */
@Controller
@RequestMapping("/ecms/bannercategory")
public class CmsBannerCategoryController extends BaseController
{
    private String prefix = "ecms/bannercategory";

    @Autowired
    private ICmsBannerCategoryService cmsBannerCategoryService;

    @RequiresPermissions("ecms:bannercategory:view")
    @GetMapping()
    public String bannercategory(CmsBannerCategory cmsBannerCategory,ModelMap mmap)
    {
    	 mmap.put("cmsBannerCategory", cmsBannerCategory);
    	mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
        return prefix + "/bannercategory";
    }

    /**
     * 查询横幅图片树列表
     */
    @RequiresPermissions("ecms:bannercategory:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CmsBannerCategory> list(CmsBannerCategory cmsBannerCategory)
    {
        List<CmsBannerCategory> list = cmsBannerCategoryService.selectCmsBannerCategoryList(cmsBannerCategory);
        return list;
    }

    /**
     * 导出横幅图片列表
     */
    @RequiresPermissions("ecms:bannercategory:export")
    @Log(title = "横幅图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsBannerCategory cmsBannerCategory)
    {
        List<CmsBannerCategory> list = cmsBannerCategoryService.selectCmsBannerCategoryList(cmsBannerCategory);
        ExcelUtil<CmsBannerCategory> util = new ExcelUtil<CmsBannerCategory>(CmsBannerCategory.class);
        return util.exportExcel(list, "横幅图片数据");
    }

    /**
     * 新增横幅图片
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsBannerCategory", cmsBannerCategoryService.selectCmsBannerCategoryById(id));
        }
        mmap.put("cmsBannerCategory", new CmsBannerCategory());
        mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
        return prefix + "/add";
    }

    /**
     * 新增保存横幅图片
     */
    @RequiresPermissions("ecms:bannercategory:add")
    @Log(title = "横幅图片", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsBannerCategory cmsBannerCategory)
    {
        return toAjax(cmsBannerCategoryService.insertCmsBannerCategory(cmsBannerCategory));
    }

    /**
     * 修改横幅图片
     */
    @RequiresPermissions("ecms:bannercategory:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsBannerCategory cmsBannerCategory = cmsBannerCategoryService.selectCmsBannerCategoryById(id);
        //初始化树结构
        ECMSUtils.initECMSTreeData(cmsBannerCategory);
        mmap.put("cmsBannerCategory", cmsBannerCategory);
        mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
        return prefix + "/edit";
    }

    /**
     * 修改保存横幅图片
     */
    @RequiresPermissions("ecms:bannercategory:edit")
    @Log(title = "横幅图片", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsBannerCategory cmsBannerCategory)
    {
        return toAjax(cmsBannerCategoryService.updateCmsBannerCategory(cmsBannerCategory));
    }

    /**
     * 删除
     */
    @RequiresPermissions("ecms:bannercategory:remove")
    @Log(title = "横幅图片", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(cmsBannerCategoryService.deleteCmsBannerCategoryById(id));
    }

    /**
     * 选择横幅图片树
     */
    @GetMapping(value = { "/selectBannercategoryTree/{id}", "/selectBannercategoryTree/" })
    public String selectBannercategoryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsBannerCategory", cmsBannerCategoryService.selectCmsBannerCategoryById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载横幅图片树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = cmsBannerCategoryService.selectCmsBannerCategoryTree();
        return ztrees;
    }
}
